<?php

/**
 * 用户登陆模型
 */

class LoginModel extends Model {
	public $table = 'user';

	public $validate = array( 
		array('username', 'nonull', '用户名不能为空', 2, 3), 
		array('password', 'nonull', '密码不能为空', 2, 3),
	 );

	/*
	 * 后台验证登陆
	 */
	public function vlogin() {
//			 	p($_POST);die;
		//执行上面的自动验证规则
		if (!$this -> create()) return false;
		//判断验证码是否正确
		$code =strtoupper($_POST['Captcha']);
		if($code != $_SESSION['code']){
			$this->error = '验证码错误';
			return false;
		}	
		//如果验证错误，返回false
		//判断用户名和密码是否正确
		$username = $_POST['username'];
		$password = md5($_POST['password']);
		//		p($password);die;
		//在数据库查询用户的信息
		$userInfo = $this -> where("username='{$username}'") -> find();
//				p($userInfo);die;
		//用户名或者密码只要有一个不相等就返回false
		if(!$userInfo || $password != $userInfo['password']){
			$this->error = "用户名或者密码错误";
			return false;
		}

		//判断用户是否呗锁定
		if ($userInfo['is_lock']) {
			$this -> error = "您已经呗锁定，请联系管理员";
			return false;
		}

		return $userInfo;

	}
		 //判断是否为后台用户
	public function isAdmin($username){
		$isAdmin = $this->where("username = '{$username}'")->field('is_admin')->find();
		return $isAdmin['is_admin'];
	}

}
?>